package com.hjm.project.channel.invoice.controller;

import java.io.IOException;
import java.util.List;

import com.hjm.framework.config.CustomConfig;
import com.hjm.project.system.job.domain.SysJob;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.channel.invoice.domain.ChannelInvoice;
import com.hjm.project.channel.invoice.service.IChannelInvoiceService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import javax.annotation.Resource;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 电子发票信息Controller
 * 
 * @author maker-platform
 * @date 2025-03-19
 */
@Controller
@RequestMapping("/channel/invoice")
public class ChannelInvoiceController extends BaseController {
    private String prefix = "channel/invoice";

    @Resource
    private IChannelInvoiceService channelInvoiceService;

    @RequiresPermissions("channel:invoice:view")
    @GetMapping()
    public String invoice() {
        return prefix + "/invoice";
    }

    /**
     * 查询电子发票信息列表
     */
    @RequiresPermissions("channel:invoice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChannelInvoice channelInvoice) {
        startPage();
        List<ChannelInvoice> list = channelInvoiceService.selectChannelInvoiceList(channelInvoice);
        return getDataTable(list);
    }

    /**
     * 导出电子发票信息列表
     */
    @RequiresPermissions("channel:invoice:export")
    @Log(title = "电子发票信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelInvoice channelInvoice) {
        List<ChannelInvoice> list = channelInvoiceService.selectChannelInvoiceList(channelInvoice);
        ExcelUtil<ChannelInvoice> util = new ExcelUtil<ChannelInvoice>(ChannelInvoice.class);
        return util.exportExcel(list, "invoice");
    }

    /**
     * 新增电子发票信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子发票信息
     */
    @RequiresPermissions("channel:invoice:add")
    @Log(title = "电子发票信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile file, ChannelInvoice channelInvoice)throws Exception {
        return toAjax(channelInvoiceService.insertChannelInvoice(file,channelInvoice));
    }

    /**
     * 修改电子发票信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        ChannelInvoice channelInvoice = channelInvoiceService.selectChannelInvoiceById(id);
        channelInvoice.setImagePath(CustomConfig.getImgUrl() + channelInvoice.getImagePath());
        mmap.put("channelInvoice", channelInvoice);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子发票信息
     */
    @RequiresPermissions("channel:invoice:edit")
    @Log(title = "电子发票信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile file, ChannelInvoice channelInvoice) throws Exception  {
        return toAjax(channelInvoiceService.updateChannelInvoice(file,channelInvoice));
    }

    /**
     * 删除电子发票信息
     */
    @RequiresPermissions("channel:invoice:remove")
    @Log(title = "电子发票信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(channelInvoiceService.deleteChannelInvoiceByIds(ids));
    }
    /**
     * 上送渠道
     */
    @Log(title = "上送渠道", businessType = BusinessType.OTHER)
    @RequiresPermissions("channel:invoice:remove")
    @PostMapping("/sendChannel")
    @ResponseBody
    public AjaxResult sendChannel(Integer id) throws IOException {
        ChannelInvoice channelInvoice = channelInvoiceService.selectChannelInvoiceById(id);
        channelInvoiceService.sendChannel(channelInvoice);
        return success();
    }
}
